package io.renren.modules.video;

import io.renren.modules.video.entity.FileEntity;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class FileUtils {
   public static BufferedInputStream bufferedInputStream;
    public static BufferedOutputStream bufferedOutputStream;

    public static void main(String[] args) throws IOException {

        String basePath = "D:\\wenjian\\测试";
        File dir = new File(basePath);

        List<File> allFileList = new ArrayList<>();
        String name = dir.getName();
        // 判断文件夹是否存在
        if (!dir.exists()) {
            System.out.println("目录不存在");
            return;
        }
        AtomicInteger index=new AtomicInteger(0);
        getAllFile(dir, allFileList);


//        allFileList.stream().forEach(
//                file -> {
//                    try {
//                        outPutFile(file,dir,index.getAndIncrement());
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//        );
        List<String>files=new ArrayList<>(allFileList.size());
        for (File file : allFileList) {

            String path=file.getPath().replace(basePath,"");
            System.out.println(path);
            files.add(path);
        }

//        System.out.println("该文件夹下共有" + allFileList.size() + "个文件");
    }

    public static void getAllFile(File fileInput, List<File> allFileList) {
        // 获取文件列表
        File[] fileList = fileInput.listFiles();
        assert fileList != null;
        for (File file : fileList) {
            if (file.isDirectory()) {
                // 递归处理文件夹
                // 如果不想统计子文件夹则可以将下一行注释掉
                getAllFile(file, allFileList);
            } else {
                // 如果是文件则将其加入到文件数组中
                allFileList.add(file);
            }
        }
    }

    public static synchronized void outPutFile(File file,File dir,int i) throws IOException {

        FileInputStream fileInputStream=new FileInputStream(file);
        bufferedInputStream=new BufferedInputStream(fileInputStream);
        String name = file.getName();
        String[] split = name.split("\\.");
        String suffx=split[1];
        String newName=i+"."+suffx;
        File newFile = new File(dir.getAbsolutePath() + "/new1");
        if (!newFile.exists()){
           newFile.mkdirs();
        }
        newFile=new File(newFile.getAbsolutePath()+"/"+newName);
        FileOutputStream outputStream=new FileOutputStream(newFile);
        bufferedOutputStream=new BufferedOutputStream(outputStream);
        byte[] b=new byte[1024];
        int len=-1;
        while ((len=bufferedInputStream.read(b,0,b.length))!=-1){
            bufferedOutputStream.write(b,0,len);
        }
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
        bufferedInputStream.close();
    }

    public static List<FileEntity> getFilePaths(String basePath){
        File dir = new File(basePath);

        List<File> allFileList = new ArrayList<>();
        String name = dir.getName();
        // 判断文件夹是否存在
        if (!dir.exists()) {
            System.out.println("目录不存在");
            return null;
        }
        AtomicInteger index=new AtomicInteger(0);
        getAllFile(dir, allFileList);
        List<FileEntity>files=new ArrayList<>(allFileList.size());
        for (File file : allFileList) {
            FileEntity fileEntity=new FileEntity();
            String path=file.getAbsolutePath().replace(basePath+"\\","");
            fileEntity.setPath(path);
           fileEntity.setFileName(file.getName());
           fileEntity.setSize(file.length());
            files.add(fileEntity);
        }
        return files;
    }
}
